package com.heima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.ClassDto;
import com.heima.pojo.dto.ClassPageQueryDTO;
import com.heima.pojo.vo.ClassPageVo;
import com.heima.service.ClassService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/vm-service/skuClass")
@Slf4j
public class ClassController {
    @Autowired
    private ClassService classService;

    //传入的参数为单个，String类型
    @PostMapping
    public Boolean addClass(@RequestBody ClassDto classDto){
        log.info("新增分类:{}",classDto);
//        classService.addClass(classDto);
        classService.save(classDto);
        return true;
    }

    @GetMapping("/search")
    public ClassPageVo pageQuery(ClassPageQueryDTO classPageQueryDTO){

//        ClassPageVo classPageVo = classService.pageQuery(classPageQueryDTO);
        //分页参数
        Page<ClassDto> page = Page.of(classPageQueryDTO.getPageIndex(), classPageQueryDTO.getPageSize());
        //设置查询条件
        QueryWrapper<ClassDto> wrapper = new QueryWrapper<>();
        wrapper.select("class_id, class_name, parent_id").like(classPageQueryDTO.getClassName()!=null,"class_name",classPageQueryDTO.getClassName());
        //分页查询
        Page<ClassDto> p = classService.page(page,wrapper);

        ClassPageVo classPageVo = new ClassPageVo();
        classPageVo.setCurrentPageRecords(p.getRecords());
        return classPageVo;
    }


    @PutMapping("/{classId}")
    public Boolean update(@PathVariable String classId,@RequestBody ClassDto classDto){
        log.info("修改分类，{},{}",classId,classDto);
        classDto.setClassId(classId);
        classService.updateById(classDto);
        return true;
    }

    @DeleteMapping("/{classId}")
    public void Delete(@PathVariable String classId){
        log.info("删除商品分类：{}",classId);
        classService.removeById(classId);
    }
}
